package com.jingan.nursinghome.regulatory.select

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import cn.android.support.v7.lib.sin.crown.base.BaseActivity
import cn.android.support.v7.lib.sin.crown.kotlin.anko.Titlebar
import cn.android.support.v7.lib.sin.crown.kotlin.base.BaseDialog
import cn.android.support.v7.lib.sin.crown.kotlin.common.px
import cn.android.support.v7.lib.sin.crown.kotlin.utils.SelectorUtils
import com.jingan.nursinghome.regulatory.data.MicrometerTable
import com.jingan.nursinghome.regulatory.model.Jinlaoyuang
import org.jetbrains.anko.*
import org.jetbrains.anko.recyclerview.v7.recyclerView
import org.jetbrains.anko.sdk25.coroutines.onClick
import java.util.ArrayList

/**
 * 养老机构选择
 * Created by 彭治铭 on 2018/7/18.
 */
class SelectOid(var activity: BaseActivity, isStatus: Boolean = true, isTransparent: Boolean = true) : BaseDialog(activity, isStatus = isStatus, isTransparent = isTransparent) {

    init {
        isDarkmode(true)
        setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right)
    }

    var recyclerView: RecyclerView? = null
    override fun onCreateView(context: Context): View? {
        return with(context) {
            UI {
                verticalLayout {
                    backgroundColor = Color.WHITE
                    //第三个参数是返回键的颜色。此时是透明色。即返回键无效
                    var topBar = Titlebar.topBar(context as Activity, "请选择巡检机构", Color.GRAY)
                    topBar?.centerTextView?.apply {
                        textColor = Color.BLACK
                    }
                    topBar.leftTextView?.onClick {
                        dismiss()//返回
                    }
                    addView(topBar?.view)
                    topBar?.shadow = true
                    recyclerView = recyclerView {
                        backgroundColor = Color.parseColor("#F2F2F2")
                        var linearLayoutManager = LinearLayoutManager(context)
                        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
                        layoutManager = linearLayoutManager
                    }.lparams {
                        width = matchParent
                        height = matchParent
                    }
                }
            }.view
        }
    }

    override fun listener() {
        val items = ArrayList<Jinlaoyuang>()
        items.add(0, Jinlaoyuang(null, "全部"))
        for (i in 0 until MicrometerTable.getInstance().jlys.size) {
            if (MicrometerTable.getInstance().jlys != null && MicrometerTable.getInstance().jlys[i] != null) {
                items.add(MicrometerTable.getInstance().jlys[i])
            }
        }
        var adatper = MyAdatper(activity, items)
        recyclerView?.adapter = adatper
    }

    override fun recycleView() {
    }

    class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var text: TextView? = null

        init {
            text = itemView?.findViewById(px.id("text"))
        }
    }

    class MyAdatper(var activity: BaseActivity, var list: MutableList<Jinlaoyuang>) : RecyclerView.Adapter<MyViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            var view = parent.context.UI {
                verticalLayout {
                    var layoutParams = ViewGroup.LayoutParams(px.realWidth().toInt(), wrapContent)
                    setLayoutParams(layoutParams)
                    gravity = Gravity.CENTER
                    textView {
                        id = px.id("text")
//                        backgroundColor = Color.WHITE
                        SelectorUtils.selectorColor(this, Color.WHITE, Color.parseColor("#FFC7BC"))
                        textColor = Color.BLACK
                        textSize = px.textSizeX(36)
                        gravity = Gravity.CENTER
                    }.lparams {
                        width = px.x(700)
                        height = px.x(100)
                        topMargin = px.x(30)
                    }
                }

            }.view
            return MyViewHolder(view)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            holder.text?.setText(list.get(position).name)
            holder.text?.onClick {
                //Log.e("test","敬老院id:\t"+list.get(position).id)
                //Log.e("test","静老院id2:\t"+MicrometerTable.getInstance().getJlyID(list.get(position).name))
                SelectHome(activity, list.get(position).id, true, true).show()
            }
        }
    }

}